package com.whu.waterworks.impl;

import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.WaterUserDAO;
import com.whu.waterworks.model.WaterUser;

public class WaterUserDAOImpl implements WaterUserDAO{
	HibernateTemplate hibernateTemplate;
	@Override
	public void save(WaterUser entity) {
		hibernateTemplate.save(entity);
	}

	@Override
	public void delete(WaterUser entity) {
		hibernateTemplate.delete(entity);
	}

	@Override
	public WaterUser update(WaterUser entity) {
		hibernateTemplate.saveOrUpdate(entity);
		if (hibernateTemplate.contains(entity)) {
			return entity;
		} 
		else {
			return null;	
		}
	}

	@Override
	public WaterUser findByName(String username) {
		List<WaterUser> waterUsers = (List<WaterUser>)hibernateTemplate.find("from WaterUser _WaterUser where _WaterUser.username='"+username+"'");
		if (waterUsers==null || waterUsers.isEmpty()) {
			return null;
		}
		else {
			return waterUsers.get(0);
		}
	}

	@Override
	public List<WaterUser> findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WaterUser> findByPassword(Object password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WaterUser> findByPermission(Object permission) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WaterUser> findByType(Object type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WaterUser> findByArea(Object area) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WaterUser> findAll() {
		List<WaterUser> waterUsers = (List<WaterUser>)hibernateTemplate.find("from WaterUser _WaterUser");
		if (waterUsers==null) {
			return null;
		}
		else {
			return waterUsers;
		}
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
